<form id="AddTemplateIdenForm" lay-filter="addGroupForm" class="layui-form model-form ">
    <input name="userId" type="hidden"/>
    <div class="layui-form-item">
        <label class="layui-form-label">目录名称</label>
        <div class="layui-input-block">
            <input name="name" placeholder="请输入目录名称" type="text" class="layui-input" maxlength="20"
                   lay-verType="tips" lay-verify="required" required/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">当前目录</label>
        <div class="layui-input-block">
            <input name="menuName" placeholder="" type="text" class="layui-input" maxlength="20"
                   lay-verType="tips" lay-verify="required" disabled/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">更改目录到</label>
        <div class="layui-input-block">
            <input name="nickName" id="edit-menu" placeholder="" type="text" class="layui-input" maxlength="20"
                   lay-verType="tips" lay-verify="required" disabled/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">选择目录</label>
        <div class="layui-input-block">
            <ul id="edit-tree" class="ztree" ></ul>
        </div>
    </div>
    <div class="layui-form-item text-right">
        <button class="layui-btn layui-btn-primary layui-btn-sm" type="button" ew-event="closePageDialog">取消</button>
        <button class="layui-btn layui-btn-sm" lay-filter="editMenu" lay-submit >确定</button>
    </div>
</form>

<script>
    layui.use(['layer', 'form', 'admin','tree'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var admin = layui.admin;
        var tree = layui.tree;
        var userInfo = getAuthority();
        var editSetting={//树设置
            identValueSetting:{//区划树设置
                edit: {
                    enable: true,
                    showRemoveBtn: false,
                    showRenameBtn: false,
                    drag: {
                        isCopy: true,
                        isMove: false,
                        prev: false,
                        next: false,
                        inner: false
                    }
                },
                data: {
                    key: {
                        children: "sub",
                        isParent: "isParent",
                        title: "name"
                    },
                    simpleData: {
                        enable: true
                    }
                },
                callback: {
                    onExpand: onEditValueTree,
                    onClick: editValueOnClick
                },
                view: {
                    showIcon: true,
                    showLine: true
                }
            },
        }
        var mUser = window.mSelUser;  // 列表页面传递的数据，也可以使用top.xxx               
        var editParentId;       
        if(userInfo){
            var access_token = localStorage.getExpire("access_token");
            var token_type = localStorage.getExpire("token_type");
            Authorization = token_type+" "+access_token;
//            getEditTree({},function (resArr) {
//                $.fn.zTree.init($("#edit-tree"), editSetting.identValueSetting, resArr);
//            });
            initIdenGroupTree();
        }
       
        // WD 2019.12.24 设置根目录为root
        if(mUser.menuData.menuId === "root"){
            mUser.menuData.menuName = "根目录";
        }
        mUser.menuData.nickName = mUser.menuData.menuName;//设置nickName值，默认当前上一级目录
        form.val('addGroupForm', mUser.menuData);  // 回显数据
        function initIdenGroupTree(){
            var dataarr = [{"data_type":0,
                "id":"root",
                "name":"根目录",
                "par_id":"",
                "version":"0",
                "sub":[]
            }];
            $.fn.zTree.init($("#edit-tree"), editSetting.identValueSetting, dataarr);
        }

        //获取树形目录的值
        function getEditTree(obj,callback) {            
            let parent_id = obj.id?obj.id:'root';             
            axios({
                method: 'get',
                url:microUrl.microObject.microMenu.microMenuList+'/'+ parent_id + '/',
                headers:{
                    'Authorization':Authorization
                }
            })
                .then(function (response) {
                    if(response.status == 200 &&response.data.message == "success"){
                        var data = response.data.data;
                        var resArr = [];
                        zhuanHuanTree(data,resArr);
                        // IdentValueTree = resArr;
                        callback(resArr)

                    }
                })
                .catch(function (error) {
                    console.log(error);
                    if(error.response.status === 401 || error.response.status === 403 || error.response.status === 406){
                        // 进行重新登陆
                        localStorage.clear();
                        top.location.href=loginPageUrl;
                    }
                });
        }
        //转换菜单集合数据
        function zhuanHuanTree(tdata,resData) {
            if(Array.isArray(tdata) && tdata.length>0){
                tdata.forEach(function(v,i){
                    var newValue=v.name;
                    resData[i]=v;
                    resData[i].title=newValue;
                    resData[i].icon = "";
                    resData[i].isParent = "true";
                    resData[i].sub = [];
                });
            }
        }

        //查询下方区划树的下级区划值（懒加载）
        function onEditValueTree(event, treeId, treeNode) {
            if(treeNode.sub.length>0||treeNode.hasChild=='0'){
                return false
            }
            getEditTree(treeNode,function (resArr) {
                const treeObj = $.fn.zTree.getZTreeObj("edit-tree");
                treeNode = treeObj.addNodes(treeNode,resArr);


            });
        }
        //选中分组值
        function editValueOnClick(event, treeId, treeNode){
            editParentId = treeNode.id      
            $('#edit-menu').val(treeNode.name)
        }
        // 表单提交事件
        form.on('submit(editMenu)', function (data) {
            var edit_formData = new FormData();
            edit_formData.append('name',data.field.name);
            edit_formData.append('id', mUser.menuData.id);
            edit_formData.append('parent_id',editParentId?editParentId:mUser.menuData.menuId);
            edit_formData.append('version', mUser.menuData.version);
            axios({
                method: 'put',
                data:edit_formData,
                url:microUrl.microObject.microMenu.editMicroMenu,
                headers:{
                    'Authorization':Authorization
                }
            })
                .then(function (res) {
                    if (res.status == 200&&res.data.message=="success") {
                        layer.closeAll('loading');
                        layer.msg("编辑成功", {icon: 1});
                        window.editIdentGroupStatus = true;
                    } else {
                        layer.msg(res.data.message, {icon: 2});
                        layer.closeAll('loading');

                    }
                    admin.closeDialog('#AddTemplateIdenForm');
                })
                .catch(function (error) {
                    console.log(error);
                    if(error.response.status === 401 || error.response.status === 403 || error.response.status === 406){
                        // 进行重新登陆
                        localStorage.clear();
                        top.location.href=loginPageUrl;
                    }
                })
            return false;

        });

    });
</script>